package com.icbc.cc.api;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.icbc.cte.logging.Log;
import com.icbc.cte.logging.LogFactory;
import com.icbc.cc.api.ApiCommonUtils;

public class ApiServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private static Log log = LogFactory.getCtpLog(ApiServlet.class);
	
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");//设置编码
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("UTF-8");
		
		PrintWriter out = response.getWriter();//获取一个输出流
		String opName = request.getParameter("opName");//获取请求参数：OPG的名称
		String param = request.getParameter("param");//携带的数据
		String osFlag = request.getParameter("osFlag");//用来判断是PC还是IOS端的请求
		HttpSession session = request.getSession(); 
		log.info("session mobileNo:" + session.getAttribute("13653644540"));
		//校验数字签名和解密
		param = ApiCommonUtils.decrypAndSign(param, ApiCommonUtils.APP_CALL_FLAG, osFlag);
		/*osFlag:app为1，pc为3*/
		JSONObject obj = (JSONObject) JSONObject.parse(param);
		String mobileNo = (String) obj.get("mobileNo");
		log.info("session mobileNo:" + session.getAttribute(mobileNo));
		log.info("ApiServlet param=" + param);
		log.info("ApiServlet opName=" + opName);
		log.info("osFlag=" + osFlag);
		String result="";
//		if("check_phone_checkCode".equals(opName)){
//			System.out.println((String)session.getAttribute(mobileNo.trim()));
//			result = ApiCommonUtils.excuteOpCheck(param,(String)session.getValue(mobileNo));
//		}else{
			result = ApiCommonUtils.excuteOp(param, opName);
		
		
		log.info("ApiServlet result:" + result);
		
		//生成数字签名并加密
		result = ApiCommonUtils.encrypAndSign(result, osFlag);
		
		out.write(result);
		out.flush();
		out.close();
	}

}
